package com.lizardmind.haofudoctor.manager

import android.annotation.SuppressLint
import android.databinding.BindingAdapter
import android.text.InputType
import android.text.Selection
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.TimeUtils
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import org.jetbrains.anko.imageURI
import java.text.SimpleDateFormat
import java.util.*

/**
 * @description  :一些不能在框架层配置的BindingAdapter方法
 * @author : Extends 15:21
 * @email : 2563892038@qq.com
 * @date : 2017/10/23
 * @change :
 * @changer :
 */
object BindingAdapterManager {
    @BindingAdapter("imgUrl")
    @JvmStatic
    fun loadImage(view: ImageView, url: String?) {
        view.setImageUrl(url)
    }

    @BindingAdapter("bind:imgOrRes","bind:defaultRes")
    @JvmStatic
    fun loadImageOrRes(view: ImageView, url: String?,resImg:Int) {
        if(url!=null){
            view.setImageUrl(url)
        }else{
            view.setImageResource(resImg)
        }

    }

    @BindingAdapter("setWeek")
    @JvmStatic
    fun setWeekDay(textView: TextView,week:Any){
        textView.text=textView.resources.getStringArray(R.array.week)[week as Int]
    }

    @SuppressLint("SimpleDateFormat")
    @BindingAdapter("timeText_millis2String")
    @JvmStatic
//    @JvmOverloads  //用于传入参数带默认值的时候
    fun timeText(textView: TextView, millis: String) {
        textView.text = SimpleDateFormat("yyyy-MM-dd").format(Date(millis.toLong() * 1000))
    }

        @BindingAdapter("passInputType")
        @JvmStatic
        fun passInputType(view: EditText, b: Boolean) {
            if (b) {
                view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                Selection.setSelection(view.text, view.text.length)//光标一直处于文字最后
            } else {
                view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                Selection.setSelection(view.text, view.text.length)//光标一直处于文字最后
            }
        }

    @BindingAdapter("format_time")
    @JvmStatic
    fun timeText(textView: TextView, time: Int) {
        textView.text = TimeUtils.millis2String(time*1000L)
    }

    @BindingAdapter("evaluation")
    @JvmStatic
    fun setEvaluation(view:TextView,i:Int){
        var txt = ""
        var dra = 0

        when(i){
            1->{
                txt="满意"
                dra= R.drawable.satisfied_1
            }
            2->{
                txt="一般"
                dra= R.drawable.general_1
            }
            3->{
                txt = "需要改善"
                dra = R.drawable.not_satisfied_1
            }
            else->{
                txt="非常满意"
                dra= R.drawable.very_satisfied_1
            }
        }

        view.text = txt
        val d = view.resources.getDrawable(dra)
        d.setBounds(0,0,d.minimumWidth,d.minimumHeight)
        view.setCompoundDrawables(d,null,null,null)
    }
}